import socket
import threading
import time
HOST = "127.0.0.1"
PORT = 9895
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    s.connect((HOST, PORT))
except:
    print("连接失败")
    
def name_server():
    HOST2 = "127.0.0.1"
    PORT2 = 9896
    s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s2.connect((HOST2, PORT2))
    except:
        print("连接失败")
    while True:
        msg = s2.recv(1024).decode('utf-8')
        if msg[:6] == "[NAME]":
            print(msg[6:])
            time.sleep(1)

def handle():
    global s
    while True:
        msg = s.recv(1024).decode('utf-8')
        if msg[:7] == "[MSGS]":
            print(msg[7:])
def print_msg():
    while True:
        msgg = s.recv(1024).decode('utf-8')
        print(msgg[6:])
name = input("请输入你的名字：")
password = input("请输入你的密码：")
login = "[LOIN]{name}:{password}".format(name=name, password=password)
print(login)
s.send(login.encode('utf-8'))
msg = s.recv(1024).decode('utf-8')
print(msg)
code = msg[:6]
if msg == "[INFO]client success":
    print("登录成功")
    #threading.Thread(target=handle).start()
    threading.Thread(target=print_msg).start()
    while True:
        msg = input("请输入你的消息：")
        s.send(f"[MSGS]{msg}".encode('utf-8'))
elif msg == "[INFO]login failed":
    print("登录失败")